package org.hot100_3;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class leetcode199 {
    public List<Integer> rightSideView(TreeNode root) {
        return BFS(root);
    }

    private List<Integer> BFS(TreeNode root){

        Deque<TreeNode> deque = new ArrayDeque<>();
        List<Integer> res = new ArrayList<>();

        if (root==null){
            return res;
        }
        deque.add(root);

        while (!deque.isEmpty()){
            int size = deque.size();
            res.add(deque.peekLast().val);
            while (size>0){
                TreeNode node = deque.poll();

                if (node.left!=null){
                    deque.offer(node.left);
                }

                if (node.right!=null){
                    deque.offer(node.right);
                }

                size--;
            }
        }

        return res;
    }
}
